<?php 
    namespace Mark\Component;
    use DateTime;
    use Mark\Component\AbstractComponent;
    
    class MarkVideoComponent extends AbstractComponent {
        protected  $entity="Mark\\Entity\\MarkVideo";
        
        
        function insert($object){
            $now = new DateTime("now");
            $object->createdAt = $now;
            $object->modifiedAt = $now;
            $this->em->persist($object);
            $this->em->flush();
        }
        
        
        function update($object){
            $now = new DateTime("now");
            $object->modifiedAt = $now;
            $this->em->persist($object);
            $this->em->flush();
        }
        
        
        function delete($id){
            $user = $this->em->find($this->entity, $id);
            $this->em->remove($user);
            $this->em->flush();
        }
        
        
        function get($id){
            return $this->em->find($this->entity, $id);
        }
        
        
        function listAll($id){
            return $this->em->getRepository($this->entity)->findAll();
        }
        
        
        public function listByVideo($idVideo, $maxResult=null){
            return $this->em->getRepository($this->entity)->listByVideo($idVideo, $maxResult);
        }
        
        public function listByVideo2($idVideo, $maxId=null, $limit=null){
            return $this->em->getRepository($this->entity)->listByVideo2($idVideo, $maxId, $limit);
        }
        
        public function getStats($idVideo){
            return $this->em->getRepository($this->entity)->getStats($idVideo);
        }
        
        public function getStats2($idVideo){
            $stats = $this->getStats($idVideo);
            $result = array();
            $result["nb"] = ($stats==null||count($stats)==0||!isset($stats[0]["nb"])||$stats[0]["nb"]==null)?0:$stats[0]["nb"];
            $result["total"] = ($stats==null||count($stats)==0||!isset($stats[0]["total"])||$stats[0]["total"]==null)?0:$stats[0]["total"];
            return $result;
        }
        
        public function getByMarkId($id){
            return $this->em->getRepository($this->entity)->getByMarkId($id);
        }
        
        
    }
